package com.htfyun.imgshow;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.htfyun.Constants;
import com.htfyun.ScanMyPictureDirManager;
import com.squareup.picasso.Picasso;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.geometerplus.zlibrary.ui.android.R;

import java.io.File;
import java.util.List;

import uk.co.senab.photoview.PhotoViewAttacher;

/**
 * Created by sst on 2017/12/28.
 */
@EActivity(R.layout.htfyun_activity_imgshow)
public class ImageViewShowActivity extends AppCompatActivity {

    @ViewById
    ImageView imageView;

    private PhotoViewAttacher attacher;
    
    private int currentImagePosition;
    List<File> imgFileList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
    }

    @Override
    protected void onResume() {
        super.onResume();

        currentImagePosition = getIntent().getIntExtra(Constants.CURRENT_IMAGE_POSITION, 0);
        imgFileList = ScanMyPictureDirManager.INSTANCE.getFiles();

        File imgNow = imgFileList.get(currentImagePosition);

        ActionBar bar = getSupportActionBar();
        if (bar != null) {
            bar.setDisplayHomeAsUpEnabled(true);
        }

        attacher = new PhotoViewAttacher(imageView);
        attacher.setZoomTransitionDuration(1);

        attacher.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() {
            @Override
            public void onPhotoTap(View view, float x, float y) {
                int item = currentImagePosition;
                int total = imgFileList.size();

                if (x < 0.33f && item > 0) {
                    item -= 1;
                } else if (x >= 0.67f && item < total - 1) {
                    item += 1;
                }

                currentImagePosition = item;
                File imgNow = imgFileList.get(currentImagePosition);
                intoImage(imgNow);

            }

        });
        attacher.update();

        intoImage(imgNow);
        
    }

    @AfterViews
    void afterViews() {
        
    }

    private void intoImage(File img) {

        setTitle(img.getName());
        
        Picasso.with(this)
                .load(img)
                .error(R.drawable.default_error)
                .fit()
                .tag("imgShow")
                .into(imageView);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        Picasso.with(this).cancelTag("imgShow");
        attacher.cleanup();
        attacher = null;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        //android.R.id.home对应应用程序图标的id
        if (item.getItemId() == android.R.id.home) {
            ActivityCompat.finishAfterTransition(this);
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
